var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
require('dotenv').config();
const adminAuth = require('./middlewares/admin-auth');
const userAuth = require('./middlewares/user-auth');
const cors = require('cors')

// 后台路由文件
const adminArticlesRouter = require('./routes/admin/articles');
const adminCategoriesRouter = require('./routes/admin/categories');
const adminSettingRouter = require('./routes/admin/setting');
const adminUsersRouter = require('./routes/admin/users');
const adminCoursesRouter = require('./routes/admin/courses');
const adminChartsRouter = require('./routes/admin/charts');
const adminAuthRouter = require('./routes/admin/auth');


var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
const categoryRouter = require('./routes/categories');
const courseRouter = require('./routes/courses');
const chapterRouter = require('./routes/chapters');
const articleRouter = require('./routes/articles');
const settingsRouter = require('./routes/settings');
const searchRouter = require('./routes/search');
const authRouter = require('./routes/auth');
const likesRouter = require('./routes/likes');

var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// CORS 跨域配置
app.use(cors());
// 后台路由配置
app.use('/admin/articles', adminAuth, adminArticlesRouter);
app.use('/admin/categories', adminAuth, adminCategoriesRouter);
app.use('/admin/setting', adminAuth, adminSettingRouter);
app.use('/admin/users', adminAuth, adminUsersRouter);
app.use('/admin/courses', adminAuth, adminCoursesRouter);
app.use('/admin/charts', adminAuth, adminChartsRouter);
app.use('/admin/auth', adminAuthRouter);

app.use('/', indexRouter);
app.use('/users', userAuth, usersRouter);
app.use("/categories", userAuth, categoryRouter)
app.use("/courses", userAuth, courseRouter)
app.use("/chapters", userAuth, chapterRouter)
app.use("/articles", userAuth, articleRouter)
app.use('/settings', userAuth, settingsRouter);
app.use('/search', userAuth, searchRouter);
app.use('/auth', authRouter);
app.use('/likes', userAuth, likesRouter);
module.exports = app;
